/**
 * 系统管理  系统环境变量
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">诊所管理</el-breadcrumb-item>
      <el-breadcrumb-item>养生知识</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form style="width: 380px;margin-top: 20px;margin-bottom: -10px">
      <el-form-item>
        <el-input size="medium" v-model="editorTitle" placeholder="请输入标题"></el-input>
      </el-form-item>
    </el-form>
    <div id="rich">
    </div>
    <div class="pre-view-box">
      <img src="../../../static/phoneImg.png" alt="" />
      <div class="pre-view" v-html="editorContent"></div>
    </div>
    <div style="margin-top: 20px">
      <el-button size="medium" type="primary" icon="el-icon-plus" @click="add">添加</el-button>
      <div style="padding: 5px"></div>
      <el-button size="medium" type="info" icon="el-icon-view" @click="view">预览</el-button>
      <div style="padding: 5px"></div>
      <el-button size="medium" type="success" icon="el-icon-edit" @click="save">保存</el-button>
    </div>
    <div class="list">
      <div class="item" v-for="(item,index) in list" :class="editorId === item.id?'item_choose':''" @click="chooseItem(index, item.id)">
        <span style="font:18px Pingfang SC;" >{{item.title}}</span>
        <br>
<!--        <span style="font:14px Pingfang SC;color: #929292;padding: 0px" v-html="item.content"></span>-->
      </div>
    </div>
  </div>
</template>

<script>
import E from 'wangeditor'
import { variableList, variableSave, variableDelete } from '../../api/userMG'
import Pagination from '../../components/Pagination'
import {addHealth, getAllHealth, getHealth, updateHealth} from "../../api";
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editor: {},
      editorId: '',
      editorContent: '',
      editorTitle: '',
      list: [],
      addFlag: false
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  watch: {},

  /**
   * 创建完毕
   */

  created() {
    this.getData()
  },

  mounted() {
    this.editor = new E('#rich')
    this.editor.config.height = 650
    // 配置 server 接口地址
    this.editor.config.showLinkImg = false
    this.editor.config.uploadFileName = 'file';
    this.editor.config.uploadImgServer = 'https://mp.wwtsz.com:8812/uploadEditor'
    this.editor.config.uploadImgAccept = ['jpg', 'jpeg', 'png', 'gif', 'bmp']
    this.editor.create()
  },

  /**
   * 里面的方法只有被调用才会执行
   */

  methods: {
    getData: function () {
      let _this = this
      getAllHealth()
        .then(res => {
          console.log(res)
          _this.list = res.result
          _this.editor.txt.html(res.result[0].content)
          _this.editorId = res.result[0].id
          _this.editorContent = res.result[0].content
          _this.editorTitle = res.result[0].title
        })
        .catch(err => {

        })
    },
    view: function () {
      this.editorContent = this.editor.txt.html()
    },
    save: function () {
      let _this = this
      if (_this.addFlag) {
        let params = new URLSearchParams()
        params.append('title', _this.editorTitle)
        params.append('content', _this.editor.txt.html())
        addHealth(params)
          .then(res => {
            if(res.result === true) {
              _this.getData()
              _this.$notify({
                title: '成功',
                message: '保存成功',
                type: 'success'
              })
            } else {
              _this.$notify.error({
                title: '错误',
                message: '保存失败'
              })
            }
          })
          .catch(err => {
            _this.$notify.error({
              title: '错误',
              message: '保存失败'
            })
          })
      } else {
        let params = new URLSearchParams()
        params.append('id', _this.editorId)
        params.append('title', _this.editorTitle)
        params.append('content', _this.editor.txt.html())
        updateHealth(params)
          .then(res => {
            console.log(res)
            if(res.result === true) {
              _this.getData()
              _this.$notify({
                title: '成功',
                message: '保存成功',
                type: 'success'
              })
            } else {
              _this.$notify.error({
                title: '错误',
                message: '保存失败'
              })
            }
          })
          .catch(err => {
            _this.$notify.error({
              title: '错误',
              message: '保存失败'
            })
          })
      }
      this.addFlag = false
    },
    add: function () {
      // this.list.unshift({})
      this.editor.txt.html("")
      this.editorContent = ""
      this.editorTitle = ""
      this.addFlag = true
    },
    chooseItem: function (index, id) {
      let _this = this
      this.editor.txt.html(_this.list[index].content)
      this.editorId = id
      this.editorContent = this.list[index].content
      this.editorTitle = this.list[index].title
    }
  }
}
</script>

<style scoped>
.list{
  clear: left;
  position: absolute;
  /*float: right;*/
  top: 170px;
  right: 20px;
  width: 350px;
  height: 770px;
  box-shadow: 0px 0px 10px 5px rgba(0,0,0,.1);
  background-color: #FFF;
  overflow: auto;
}
.list .item{
  clear: left;
  width: 91%;
  padding: 15px;
  border-bottom: solid 1px rgba(0,0,0,.1);
  transition: all .25s;
  cursor: pointer;
}
.list .item_choose{
  background-color: rgba(123, 123, 123, 0.1);
}
.list .item:hover{
  background-color: rgba(0,0,0,.1);
}
#rich{
  width: 380px;
  float: left;
}
.pre-view-box{
  position: relative;
  margin-left: 40px;
  margin-right: 40px;
  float: left;
}
.pre-view{
  position: absolute;
  top: 95px;
  left: 26px;
  width: 306px;
  height: 579px;
  padding:  0px 10px;
  background-color: #FFF;
  overflow: auto;
}
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

